﻿//#include rfm
//#include rfm.ui.Page

(function() {

var _currentPage = null;

function _onorientationchange(e) {
	if (_currentPage && _currentPage.onorientationchange) {
		_currentPage.onorientationchange(e, rfm.Viewport.isPortrait());
	}
}

/**
 * Basic navigation state manager
 */
rfm.ui.PageManager = {
    /**
     * Gets the current page state object
     * @returns {rfm.ui.Page} The current page (if any)
     */
    getCurrentPage: function() {
        return _currentPage;
    },
    /**
     * Transitions to a new page state
     * @param {String} pageTypeName The registered name of the page type to use for the new page
     * @param {Element} pageElement The DOM element representing the page
     * @param {Object} pageData Data to feed to the page
     * @param {Element} container The DOM element that contains pages
     * @param {Function} oncomplete (Optional) A function to execute when the state transition has completed
     * @returns {rfm.ui.Page} The new page
     */
    loadPage: function(pageTypeName, pageElement, pageData, container, oncomplete) {
    	var pageType = rfm.resolve(pageTypeName),
        	nextPage = new pageType(pageElement, pageData);
        nextPage.preload(function() {
            if (_currentPage) {
                _currentPage.transitionOut();
            }
            nextPage.transitionIn(container, oncomplete);
            _currentPage = nextPage;
        });
        return nextPage;
    },
    /**
     * Prepares the page manager for cleanup
     */
    finalize: function() {
        if (_currentPage) {
            _currentPage.finalize();
        }
        _pageTypes
        	= _currentPage
        	= null;
    }
};

if (rfm.Viewport) {
	rfm.Viewport.bind_orientationchange(_onorientationchange);
}

})();